<?php
include_once('inc/functions.inc.php');

if(!isEmpty(getParam('operation'))){
    
    switch(getParam('operation')){
        case 'contenido': {
            fntAcademicoNivelesContenido($db);
            break;
        }
        case 'reporte': {
            fntAcademicoNivelesReporte($db);
            break;
        }
        case 's': {
            fntAcademicoNivelesGrabar($db);
            break;
        }
    }
    die();
}

function fntAcademicoNivelesContenido($db){
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strJornada = "";
    $arrJornada = array();
    $arrDias = array();
    $strSelected = "";
    //VARIBLES
    
    $strId = isset($_POST["hdnAcademicoNiveles_Id"]) ? $_POST["hdnAcademicoNiveles_Id"] : "";
    if( !empty($strId) ) {
        $strQuery = "SELECT * FROM cllg_academico_niveles WHERE ID = '{$strId}'";
        $qTMP = $db->query($strQuery);
        while ( $rTMP = $db->fetch_array($qTMP) ) { 
            $arrInfo = $rTMP;        
        }
        $db->free_result($qTMP);
    }
    
    $strCodigo = isset($arrInfo["CODIGO"]) ? $arrInfo["CODIGO"] : "";
    $strNombre = isset($arrInfo["NOMBRE"]) ? $arrInfo["NOMBRE"] : "";
    $strActivo = isset($arrInfo["ACTIVO"]) ? $arrInfo["ACTIVO"] : "";
    $strJornada = isset($arrInfo["JORNADA"]) ? $arrInfo["JORNADA"] : "";
    $strDiaInicial = isset($arrInfo["DIA_INICIAL"]) ? $arrInfo["DIA_INICIAL"] : "";
    $strDiaFinal = isset($arrInfo["DIA_FINAL"]) ? $arrInfo["DIA_FINAL"] : "";
    $strHoraInicial = isset($arrInfo["HORA_INICIAL"]) ? $arrInfo["HORA_INICIAL"] : "";
    $arrHorarioInicial = explode(":", $strHoraInicial);
    $strHoraFinal = isset($arrInfo["HORA_FINAL"]) ? $arrInfo["HORA_FINAL"] : "";
    $arrHorarioFinal = explode(":", $strHoraFinal);
    
    $arrJornada["MATUTINA"] = "MATUTINA";
    $arrJornada["VESPERTINA"] = "VESPERTINA";
    $arrJornada["NOCTURNA"] = "NOCTURNA";
    
    $arrDias["DOMINGO"] = "DOMINGO";
    $arrDias["LUNES"] = "LUNES";
    $arrDias["MARTES"] = "MARTES";
    $arrDias["MIERCOLES"] = "MIERCOLES";
    $arrDias["JUEVES"] = "JUEVES";
    $arrDias["VIERNES"] = "VIERNES";
    $arrDias["SABADO"] = "SABADO";
    
    ?>
    <br>
    <br>
    <div style="width: 100px; height: 30px; float: left;"><b>Codigo</b></div>
    <div style="width: 400px; height: 30px; float: left;"><input type="text" name="txtAcademicoNiveles_codigo" value="<?php print $strCodigo; ?>"></div>
    <div style="width: 100px; height: 30px; float: left;"><b>Nombre</b></div>
    <div style="width: 400px; height: 30px; float: left;"><input type="text" name="txtAcademicoNiveles_Nombre" value="<?php print $strNombre; ?>"></div>
    <div style="width: 100px; height: 30px; float: left;"><b>Jornada</b></div>
    <div style="width: 400px; height: 30px; float: left;">
        <select name="txtAcademicoNiveles_JORNADA" style="width: 100px;">
            <?php
            reset($arrJornada);
            while($arrTMP = each($arrJornada) ) {
                $strSelected = ( $arrTMP["key"] == $strJornada ) ? "selected" : "";  
                ?>
                <option value="<?php print $arrTMP["key"]; ?>" <?php print $strSelected; ?> ><?php print $arrTMP["key"]; ?></option>
                <?php                
            }
            ?>
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Dias</b></div>
    <div style="width: 120px; height: 30px; float: left;">
        <select name="txtAcademicoNiveles_DIA-INICIAL" style="width: 100px;">
            <?php
            reset($arrDias);
            while($arrTMP = each($arrDias) ) {
                $strSelected = ( $arrTMP["key"] == $strDiaInicial ) ? "selected" : "";  
                ?>
                <option value="<?php print $arrTMP["key"]; ?>" <?php print $strSelected; ?> ><?php print $arrTMP["key"]; ?></option>
                <?php                
            }
            ?>
        </select>
    </div>
    <div style="width: 30px; height: 30px; float: left;"> A </div>
    <div style="width: 250px; height: 30px; float: left;">
        <select name="txtAcademicoNiveles_DIA-FINAL" style="width: 100px;">
            <?php
            reset($arrDias);
            while($arrTMP = each($arrDias) ) {
                $strSelected = ( $arrTMP["key"] == $strDiaFinal ) ? "selected" : "";  
                ?>
                <option value="<?php print $arrTMP["key"]; ?>" <?php print $strSelected; ?> ><?php print $arrTMP["key"]; ?></option>
                <?php                
            }
            ?>
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Horario</b></div>
    <div style="width: 120px; height: 30px; float: left;">
        <select name="sltAcademicoNiveles_HORA-INICIAL">
            <?php
            for( $intTMP = 0; $intTMP < 24; $intTMP++ ) {
                $intTMP = ( $intTMP < 10 ) ? "0".$intTMP : $intTMP;
                $strSelected = ( $intTMP == $arrHorarioInicial[0] ) ? "selected" : "";
                ?>
                <option value="<?php print $intTMP; ?>" <?php print $strSelected; ?> ><?php print $intTMP; ?></option>
                <?php
            }
            ?>
        </select>&nbsp;:&nbsp;
        <select name="sltAcademicoNiveles_MINUTOS-INICIAL">
            <?php
            for( $intTMP = 0; $intTMP < 60; $intTMP++ ) {
                $intTMP = ( $intTMP < 10 ) ? "0".$intTMP : $intTMP;
                $strSelected = ( $intTMP == $arrHorarioInicial[1] ) ? "selected" : "";
                ?>
                <option value="<?php print $intTMP; ?>" <?php print $strSelected; ?> ><?php print $intTMP; ?></option>
                <?php
            }
            ?>
        </select>
        <input type="hidden" name="txtAcademicoNiveles_HORA-INICIAL" value="<?php print $strHoraInicial; ?>">
    </div>
    <div style="width: 30px; height: 30px; float: left;"> A </div>
    <div style="width: 250px; height: 30px; float: left;">
        <select name="sltAcademicoNiveles_HORA-FINAL">
            <?php
            for( $intTMP = 0; $intTMP < 24; $intTMP++ ) {
                $intTMP = ( $intTMP < 10 ) ? "0".$intTMP : $intTMP;
                $strSelected = ( $intTMP == $arrHorarioFinal[0] ) ? "selected" : "";
                ?>
                <option value="<?php print $intTMP; ?>" <?php print $strSelected; ?> ><?php print $intTMP; ?></option>
                <?php
            }
            ?>
        </select>&nbsp;:&nbsp;
        <select name="sltAcademicoNiveles_MINUTOS-FINAL">
            <?php
            for( $intTMP = 0; $intTMP < 60; $intTMP++ ) {
                $intTMP = ( $intTMP < 10 ) ? "0".$intTMP : $intTMP;
                $strSelected = ( $intTMP == $arrHorarioFinal[1] ) ? "selected" : "";
                
                ?>
                <option value="<?php print $intTMP; ?>" <?php print $strSelected; ?> ><?php print $intTMP; ?></option>
                <?php
            }
            ?>
        </select>
        <input type="hidden" name="txtAcademicoNiveles_HORA-FINAL" value="<?php print $strHoraFinal; ?>">
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Activo</b></div>
    <div style="width: 400px; height: 30px; float: left;">Si&nbsp;<input type="radio" name="radAcademicoNiveles_activo" value="Y" <?php print ($strActivo=="Y")? "checked" : ""; ?>>&nbsp;No<input type="radio" name="radAcademicoNiveles_activo" value="N" <?php print ($strActivo=="N")? "checked" : ""; ?>></div>
    <script type="text/javascript">
    $(function() {
        $("[name='sltAcademicoNiveles_HORA-INICIAL'],[name='sltAcademicoNiveles_MINUTOS-INICIAL']").change(function() {
            strHoraInicial = $("[name='sltAcademicoNiveles_HORA-INICIAL']").val()+":"+$("[name='sltAcademicoNiveles_MINUTOS-INICIAL']").val();
            $("[name='txtAcademicoNiveles_HORA-INICIAL']").val(strHoraInicial);
        });
        $("[name='sltAcademicoNiveles_HORA-FINAL'],[name='sltAcademicoNiveles_MINUTOS-FINAL']").change(function() {
            strHoraFinal = $("[name='sltAcademicoNiveles_HORA-FINAL']").val()+":"+$("[name='sltAcademicoNiveles_MINUTOS-FINAL']").val();
            $("[name='txtAcademicoNiveles_HORA-FINAL']").val(strHoraFinal);
        });
    });
    </script>
    <?php
    
}
function fntAcademicoNivelesReporte($db){

    //VARIBLES
    $strId = "";
    $strQuery = "";
    //VARIBLES
    
    $strQuery = "SELECT * FROM cllg_academico_niveles ORDER BY NOMBRE";
    $qTMP = $db->query($strQuery);
    $intNumRows = $db->num_rows;
    
    ?>
    <style type="">
    table {
        border-collapse: collapse;
    }
    table td{
        border: 1px solid #AAAAAA;        
    }
    .oddtr {
        background-color:#FFFFFF;
    }
    .eventr {
        background-color:#FFFFFF;
    }
    .trover {
        background-color: #ECEAE0;
    }
    .trclick {
        background-color: #ECEAE0;
    }
    </style>
    <br>
    <br>
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tbody>
            <td align="center" width="20%" style="background-color: #ECEAE0;"><b>Codigo</b></td>
            <td width="80%" style="background-color: #ECEAE0;"><b>Nombre</b></td>
        </tbody>
    </table>
    <table width="100%" cellpadding="2" cellspacing="0" border="0" id="tblAcademicoNiveles">   
        <?php    
        if( $intNumRows ) {
        
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                ?>
                <tr id="<?php print $rTMP["ID"]; ?>">
                    <td align="center" width="20%"><?php print $rTMP["CODIGO"]; ?></td>
                    <td width="80%"><?php print $rTMP["NOMBRE"]; ?></td>
                </tr>        
                <?php
            }
            
        }
        $db->free_result($qTMP);
        ?>
    </table>
    <script type="">
    //these two line adds the color to each different row
    $("#tblAcademicoNiveles tr:even").addClass("eventr");;
    $("#tblAcademicoNiveles tr:odd").addClass("oddtr");;
    //handle the mouseover , mouseout and click event
    $("#tblAcademicoNiveles tr").mouseover(function() {
        $(this).addClass("trover");
    }).mouseout(function() {
        $(this).removeClass("trover");
    }).click(function() {
        //$("tr[id!='"+$(this).attr("id")+"']").removeClass("trover");
        //$(this).addClass("trover");        
        //$(this).toggleClass("trclick");
        $("input[name='hdnAcademicoNiveles_Id']").val( $(this).attr("id") );
        fntAcademicoNivelesContenido();
    });
    </script>
    <?php
    
}
function fntAcademicoNivelesGrabar($db){

    $strId = isset($_POST["hdnAcademicoNiveles_Id"]) ? $_POST["hdnAcademicoNiveles_Id"] : "";
    $strFields = "";
    $strValues = "";
    
    while( $arrPost = each($_POST) )  {
        $arrExplode = explode( "_", $arrPost["key"]);
        
        if( $arrExplode[0] == "txtAcademicoNiveles" ) {
            $arrExplode[1] = str_replace("-", "_",$arrExplode[1]);
            if( empty($strId) ) {
                $strFields .= (empty($strFields)) ? "" : ","; 
                $strFields .=  strtoupper( $arrExplode[1] );
                
                $strValues .= empty( $strValues ) ? "" : ",";
                $strValues .= "'{$arrPost["value"]}'";
            }
            else {
                $strFields .= (empty($strFields)) ? "" : ",";
                $strFields .=  strtoupper( $arrExplode[1] ). " = '{$arrPost["value"]}'"; 
                
            }
        }
        
    }
    if( empty($strId) ) {
        $db->query("INSERT INTO cllg_academico_niveles ({$strFields}) VALUES ({$strValues})");
    }
    else {
        $db->query("UPDATE cllg_academico_niveles SET {$strFields} WHERE ID = '{$strId}'");
    }
    
    
}
?>
